home *** CD-ROM | disk | FTP | other *** search
- /*
- Commodore 64 Emulator v0.4 Earle F. Philhower III
- Copyright (C) 1993-4 (st916w9r@dunx1.ocs.drexel.edu)
-
- This program is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 2 of the License, or
- (at your option) any later version.
-
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
-
- You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- */
-
- #include "Processor.h"
- #include "Memory.h"
- #include "Error.h"
- #include "Traps.h"
-
- #define MAXTRAPS 16
- static trap trapList[MAXTRAPS];
- static byte numTraps;
-
- int TrapInitialize()
- {
- numTraps=0;
- return kNoError;
- }
-
- int AddTrap(trap t)
- {
- /* If we are at out limit for traps, then internal error */
- if (numTraps==MAXTRAPS) InternalError(kTrapStorageOverflow);
-
- /* Check to see if the ROMs and the trap agree */
- if (t.check[0]!=ByteAt(t.addr)) return kWrongROMError;
- if (t.check[1]!=ByteAt(t.addr+1)) return kWrongROMError;
- if (t.check[2]!=ByteAt(t.addr+2)) return kWrongROMError;
-
- /* Add this trap to out list */
- trapList[numTraps++]=t;
-
- /* Either modify the low ROM or high ROM, as per address specified */
- if (t.addr < 0xc000) loROM[t.addr&0x1fff]=0xff;
- else hiROM[t.addr&0x1fff]=0xff;
-
- return kNoError;
- }
-
- int DeleteTrap(word addr)
- {
- int x, y;
-
- /* Search through traps added for the address to remove from */
- for (x=0; x<numTraps; x++)
- if (trapList[x].addr==addr) {
-
- /* Fix ROM to its initial state */
- if (addr <= 0xc000) loROM[addr&0x1fff]=trapList[x].check[0];
- else hiROM[addr&0x1fff]=trapList[x].check[0];
-
- /* Shift all of the traps down one slot */
- for (y=x; y<numTraps-1; y++) trapList[y]=trapList[y+1];
-
- numTraps--;
- return kNoError; }
-
- /* Attempted to remove a trap not added */
- InternalError(kTrapNotInstalled);
- }
-
- void TrapExecute()
- {
- int x;
-
- /* Search for a trap at the current pc location, and if so execute it */
- for (x=0; x<numTraps; x++)
- if (trapList[x].addr==(pc-1)) {
- (*trapList[x].call)();
- return; }
- /* This is not OUR trap code, so it's a nonimplemented instruction */
- ini();
- }
-